package com.itstudy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itstudy.mapper.ArticleMapper;
import com.itstudy.pojo.Article;
import com.itstudy.pojo.PageBean;
import com.itstudy.service.ArticleService;
import com.itstudy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void insert(Article article) {
        Map<String,Object> map=ThreadLocalUtil.get();
        article.setCreateUser((Integer) map.get("id"));
        articleMapper.insert(article);
    }

    @Override
    public PageBean<Article> pageQuery(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        PageBean<Article> pageBean=new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Page<Article> list = articleMapper.pageQuery(categoryId,state,id);
        pageBean.setTotal(list.getTotal());
        pageBean.setItems(list.getResult());
        return pageBean;
    }

    @Override
    public Article getDetail(Integer id) {
        return articleMapper.getDetail(id);
    }

    @Override
    public void updateArticle(Article article) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        article.setCreateUser(id);
        articleMapper.updateArticle(article);
    }

    @Override
    public int deleteById(Integer id) {
        return articleMapper.deleteById(id);
    }
}
